package com.imu.purse.modules.app.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Random;

import com.google.common.collect.Lists;
import com.imu.constant.Constant;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.xiaoleilu.hutool.util.ArrayUtil;
import com.xiaoleilu.hutool.util.NumberUtil;
import com.xiaoleilu.hutool.util.StrUtil;
import com.imu.core.base.BaseService;
import com.imu.purse.modules.app.dao.CoreMapper;
import com.imu.purse.modules.app.entity.CoreEntity;
import com.imu.purse.modules.app.service.CoreService;
import org.springframework.util.CollectionUtils;

/**
 * 
 * @author sunhongwei
 * @email sunhongwei@jshijian.com
 * @date 2018-04-16 16:15:31
 * Copyright (c) 2017 武汉极时间科技有限公司 版权所有 
 * Jshijian aloha CO.,LTD. All Rights Reserved. 
 */
@Service
public class CoreServiceImpl extends BaseService<CoreEntity> implements CoreService 
{
	@Override
    @Transactional
    public int save(final CoreEntity core)
	{
		//core.setTimeCreate(new Date());
    	//core.setTimeUpdate(new Date());
		core.setTimeCreate(new Date());
		int i = super.insert(core);
		CoreEntity tmpEntity = new CoreEntity();
		BeanUtils.copyProperties(core,tmpEntity);
		tmpEntity.setTimeCreate(null);
		tmpEntity.setUserCreate(null);
		tmpEntity.setTimeUpdate(null);
		tmpEntity.setUserUpdate(null);
    	redisTemplate.opsForList().leftPush(Constant.CORE_MONEY_KEY, JSON.toJSONString(tmpEntity));
    	return i;
	}
	
	@Override
	@Transactional
    public int update(final CoreEntity core) 
    {
    	CoreEntity tmpEntity = mapper.selectByPrimaryKey(core);
    	tmpEntity.setUserCreate(null);
    	tmpEntity.setTimeCreate(null);
    	tmpEntity.setUserUpdate(null);
    	tmpEntity.setTimeUpdate(null);
		redisTemplate.opsForList().remove(Constant.CORE_MONEY_KEY, 0, JSON.toJSONString(tmpEntity));
    	//core.setTimeUpdate(new Date());
		BeanUtils.copyProperties(core,tmpEntity);
		tmpEntity.setUserCreate(null);
		tmpEntity.setTimeCreate(null);
		tmpEntity.setUserUpdate(null);
		tmpEntity.setTimeUpdate(null);
		redisTemplate.opsForList().leftPush(Constant.CORE_MONEY_KEY, JSON.toJSONString(tmpEntity));

		return mapper.updateByPrimaryKeySelective(core);
    }
    
    @Override
	@Transactional
    public int saveUpdate(final CoreEntity core)
    {
    	int len = 0;
    	
    	if(1 > (len = this.update(core)))
    	{
        	len = this.insert(core);
    	}
    	
    	return len;
    }
    
 	@Override
	@Transactional
    public boolean delete(final String ...ids)
    {
    	if(ArrayUtil.isEmpty(ids)) return false;
    	for (String id : ids) 
    	{
    		if(StrUtil.isBlank(id)) continue;
    		
    		if(NumberUtil.isNumber(id))
    		{
    			CoreEntity entity = mapper.selectByPrimaryKey(Long.parseLong(id));
				CoreEntity tmpEntity = new CoreEntity();
				BeanUtils.copyProperties(entity,tmpEntity);
				tmpEntity.setTimeCreate(null);
				tmpEntity.setUserCreate(null);
				tmpEntity.setTimeUpdate(null);
				tmpEntity.setUserUpdate(null);
    	    	redisTemplate.opsForList().remove(Constant.CORE_MONEY_KEY, 0, JSON.toJSONString(tmpEntity));
        		mapper.deleteByPrimaryKey(Long.valueOf(id));
    		}
		}
    	
    	return true;
    }
    
    @Override
    public CoreEntity findById(final Integer id)
    {
    	if(null == id) return null;
    	return mapper.selectByPrimaryKey(Long.parseLong(id.toString()));
    }
    
    @Override
    public List<CoreEntity> queryList(final CoreEntity core)
    {
    	if (core.getPage() != null && core.getLimit() != null)
    	{
            PageHelper.startPage(core.getPage(), core.getLimit());
        }
    	
    	return mapper.findByPage(core);
    }

	public String  getCoreAddress(){
		//随机获取核心钱包的服务器
		List<String> coreList = redisTemplate.opsForList().range(Constant.CORE_MONEY_KEY,0,-1);
		if(CollectionUtils.isEmpty(coreList)){
			coreList = Lists.newArrayList();
			List<CoreEntity> entityList = mapper.getCoreAddress();
			if(!CollectionUtils.isEmpty(entityList)){
				for(CoreEntity entity : entityList){
					coreList.add(JSON.toJSONString(entity));
					redisTemplate.opsForList().leftPush(Constant.CORE_MONEY_KEY,JSON.toJSONString(entity));
				}
			}
		}
		//随机取一个核心钱包的地址
		Random rand = new Random();
		int curSel = rand.nextInt(coreList.size());
		return coreList.get(curSel);
	}

	@Autowired
	private CoreMapper mapper;
	
	@Autowired
    private RedisTemplate<String, String> redisTemplate;
}
